package com.akuhome.resource.job;

import com.akuhome.resource.entity.Project;
import com.akuhome.resource.enums.ProjectStatus;
import com.akuhome.resource.enums.ResultEnum;
import com.akuhome.resource.exception.ResourceException;
import com.akuhome.resource.service.entity.ProjectService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

@Component
@DisallowConcurrentExecution
public class UpdateProjectStatusJob extends QuartzJobBean {

    Logger logger = LoggerFactory.getLogger(UpdateProjectStatusJob.class);

    @Autowired
    ProjectService projectService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String uid = dataMap.getString("uid");
        if(uid != null){
            Project project = projectService.findByUId(uid);
            ProjectStatus projectStatus;
            logger.info("uid为 "+uid+" 的项目状态为 "+project.getProjectStatus().name());
            if(project.getProjectStatus().equals(ProjectStatus.CONFIRMED)){
                projectStatus = ProjectStatus.COMPLETE;
            }else{
                projectStatus = ProjectStatus.OBSOLETE;
            }
            logger.info("正在将uid为 "+uid+" 的项目设置状态为 "+projectStatus.name());
            projectService.updateProjectStatus(uid,projectStatus);
            logger.info("更新成功,已将uid为 "+uid+" 的项目设置状态为 "+projectStatus.name());
        }else{
            throw new ResourceException(ResultEnum.DATA_MAP_ERROR);
        }
    }
}
